package com.cjlgb.design.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author Bin
 * @date 2019-05-13 16:33
 * description:日志记录拦截器
 */
@Slf4j
@Component
public class LoggingGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        Mono<Void> mono = chain.filter(exchange);

        log.info("================================");

        exchange.getRequest().getHeaders().forEach((key,list) -> {
            System.out.print(key + "\t");
            list.forEach(System.out::print);
            System.out.println();
        });

        return mono.then(Mono.fromRunnable(() -> log.info("日志记录拦截器,后置处理")));
    }

    @Override
    public int getOrder() {
        return 1;
    }

}
